package com.swu.education.service;

import com.swu.education.entity.Activity;
import com.swu.education.entity.Assignment;
import com.swu.education.entity.Course;
import com.swu.education.entity.CourseConnUser;
import com.swu.education.repository.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by nana on 2018/3/13.
 */
@Service
public class CourseService {

  @Autowired
  CourseRepository courseRepository;
  @Autowired
  CourseConnUserRepository courseConnUserRepository;
  @Autowired
  UserRepository userRepository;
  @Autowired
  AssignmentRepository assignmentRepository;
  @Autowired
  ActivityRepository activityRepository;

  public Course getCourseByCourseId(int courseId){
    return courseRepository.findOne(courseId);
  }

  public List<Course> getCourseById(int userId){
    List<Course> allCourseListById =  courseRepository.findAllByUserId(userId);
    return allCourseListById;
  }
  public Course createCourseByTeacherId(int userId,String courseName,String startTime,String endTime){
    //创建课程类并赋值
    Course course = new Course();
    course.setCourseName(courseName);
    course.setUserId(userId);
    course.setStartTime(startTime);
    course.setEndTime(endTime);
    //存储course
    courseRepository.save(course);
    return course;
  }

  public void deleteCourseById(int courseId){
    //查询所有与该课程所有的关联
    List<CourseConnUser> courseConnUsers = courseConnUserRepository.findAllByCourseId(courseId);
    //删除与用户表的关联信息
    for(int i =0;i<courseConnUsers.size();i++){
      courseConnUserRepository.delete(courseConnUsers.get(i).getConnId());
    }
    //查询与该课程有关的作业
    List<Assignment> assignments = assignmentRepository.findAllByCourseId(courseId);

    //遍历作业列表
    for(int i=0;i<assignments.size();i++){
      //查询每一个作业相关的活动
      List<Activity> activityList =
              activityRepository.findAllByAssignmentId(assignments.get(i).getAssignmentId());
      //删除活动
      for(int j=0;j<activityList.size();j++){
        activityRepository.delete(activityList.get(i).getActivityId());
      }
      //删除作业
      assignmentRepository.delete(assignments.get(i).getAssignmentId());
    }
    courseRepository.delete(courseId);
  }

  public void modifyCourseInfoById(int courseId,String courseName,String startTime,
                                   String endTime,String fileStatus){
    Course course = courseRepository.findOne(courseId);
    course.setCourseName(courseName);
    course.setStartTime(startTime);
    course.setEndTime(endTime);
    courseRepository.save(course);
    if(fileStatus=="full"){
      System.out.println("根据课程ID从关联表中查询userId集合");
      List<CourseConnUser> courseConnUsers  = new ArrayList<>();
      try {
        courseConnUsers = courseConnUserRepository.findAllByCourseId(courseId);
        System.out.println("删除user表中此课程的用户");
        for(int i=0;i<courseConnUsers.size();i++){
          userRepository.delete(courseConnUsers.get(i).getUserId());
        }
        System.out.println("删除关联表中此课程的关联信息");
        for(int i=0;i<courseConnUsers.size();i++){
          courseConnUserRepository.delete(courseConnUsers.get(i).getConnId());
        }
      }catch (Exception e){
        System.out.println("gfgf");
      }
    }
  }

  public List<Course> getStudentCourseList(int userId){
    List<CourseConnUser> courseConnUsers = courseConnUserRepository.findAllByUserId(userId);
    List<Course> courseList = new ArrayList<>();
    for (int i=0;i<courseConnUsers.size();i++){
      courseList.add(courseRepository.findOne(courseConnUsers.get(i).getCourseId()));
    }
    return courseList;
  }
}
